//
//  BallProgressView.swift
//  FromWatchToCircle
//
//  Created by 张海峰 on 2018/6/1.
//  Copyright © 2018年 张海峰. All rights reserved.
//
/*尊重OC原创作者：高少东 https://github.com/gsdios/SDProgressView.git */
/*写该demo的目的：主要是想从时钟开始，一步步窥见项目中和圆有关的东西，从而学习巩固，加深理解。
 (项目不定时更新，所以最好从下面地址获取最新demo),感觉我这个demo对你有启发或者帮助，欢迎fork 感谢star
 https://github.com/FighterLightning/FromWatchToCircle.git
 */
import UIKit
class BallProgressView: BaseProgressView {
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        let ctx: CGContext = UIGraphicsGetCurrentContext()!
        //画一个实心的白色背景
        UIColor.white.set()
        let WH: CGFloat  = defaultWH - ProgressViewItemMargin
        let x: CGFloat  = (defaultWH - WH)/2
        let y: CGFloat = (defaultWH - WH)/2
        ctx.addEllipse(in: CGRect.init(x: x, y: y, width: WH, height: WH))
        ctx.fillPath()//实心
        //画进度
        let centerXY: CGFloat = defaultWH/2 //宽高
        let radius :CGFloat = (defaultWH - ProgressViewItemMargin)/2 - 5 //半径 = 白色区域宽高/2 - 5
        UIColor.red.set()// 进度条颜色
        let startAngle: CGFloat = .pi/2 + self.progress * .pi
        let endAngle: CGFloat  = .pi/2 - self.progress * .pi
        ctx.addArc(center: CGPoint.init(x: centerXY, y: centerXY), radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true) 
        ctx.fillPath()//实心
        //要绘制的文字
        let progressStr = "\(self.progress * 100)%"
        //文字样式属性
        let style = NSMutableParagraphStyle()
        style.alignment = .center
        let attributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: defaultTextFont),
                          NSAttributedStringKey.foregroundColor: UIColor.lightGray,
                          NSAttributedStringKey.paragraphStyle: style]
        self.setCenterProgressText(text: progressStr, attributes: attributes)
    }
}
